<?php
/**
 * TomatoCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@tomatocms.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2009-2010 TIG Corporation (http://www.tig.vn)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @version 	$Id: LatestArticles.php 4551 2010-08-12 10:29:42Z huuphuoc $
 */

class Salesevent_View_Helper_LatestSalesevent extends Zend_View_Helper_Abstract 
{
	public function latestSalesevent($lang = null, $limit = 3) 
	{
	    $defaultLang = Tomato_Config::getConfig()->localization->languages->default;
        $elementDisabled = ($lang != null && $lang == $defaultLang) ? ' disabled="disabled"' : '';
        if (null == $lang) {
            $lang = $defaultLang;
        }
		
		$conn = Tomato_Db_Connection::factory()->getSlaveConnection();
		$categoryDao = Tomato_Model_Dao_Factory::getInstance()->setModule('category')->getCategoryDao();
        $categoryDao->setDbConnection($conn)->setLang($lang);
        $articleDao = Tomato_Model_Dao_Factory::getInstance()->setModule('news')->getArticleDao();
        $articleDao->setDbConnection($conn)->setLang($lang);
        
        $categories = $categoryDao->getSubCategories(Salesevent_Models_SalesEventConfig::PRODUCT);
        $articles = array();
        foreach ($categories as $category) {
        	Zend_Registry::get('logger')->log('latestSalesevent' . $category->category_id, Zend_Log::EMERG);
        	$lasteAricles = $articleDao->getByCategory($category->category_id, 0, $limit);
        	if (count($lasteAricles) > 0) {
        		Zend_Registry::get('logger')->log('latestSalesevent' . $category->name, Zend_Log::EMERG);
        		$articles[$category->name] = $lasteAricles;
        	}
        }
        if (count($articles) > 0) {
        	Zend_Registry::get('logger')->log('latestSalesevent' . count($articles), Zend_Log::EMERG);
        	$this->view->assign('articles', $articles);
        }
        
		$this->view->addScriptPath(TOMATO_APP_DIR . DS . 'modules' . DS . 'salesevent' . DS . 'views' . DS . 'scripts');
        return $this->view->render('_partial/_newersalesevent.phtml');
	}
}
